package com.behelpful.common.integration.iredis.constants;

import com.behelpful.common.core.enums.IRedisCacheEnum;
import lombok.Getter;

import java.util.concurrent.TimeUnit;

/**
 * @author Alay
 * @date 2021-09-05 18:42
 * @since
 */
@Getter
public enum CommonRedisCacheEnum implements IRedisCacheEnum {

    /**
     * 路由信息数据
     */
    ROUTE_HASH_DATA("ROUTE_HASH_DATA", 1),

    /**
     * 授权码模式授权码前缀(60,秒过期)
     */
    BEHELPFUL_OAUTH_CODE("BEHELPFUL_OAUTH_CODE_PREFIX_", 1, 60),

    ;


    /**
     * key 或 Key 前缀
     */
    private String key;

    /**
     * DB 索引
     */
    private int index;

    /**
     * 过期时间秒
     */
    private int expire;

    /**
     * 过期时间单位
     */
    private TimeUnit timeUnit;


    CommonRedisCacheEnum(String key, int index, int expire) {
        this.key = key;
        this.index = index;
        this.expire = expire;
        this.timeUnit = TimeUnit.SECONDS;
    }

    CommonRedisCacheEnum(String key, int index) {
        this.key = key;
        this.index = index;
    }

    CommonRedisCacheEnum(String key, int index, int expire, TimeUnit timeUnit) {
        this.key = key;
        this.index = index;
        this.expire = expire;
        this.timeUnit = timeUnit;
    }


    public long getExpire() {
        long expire;
        switch (this.timeUnit) {
            // 单位毫秒
            case MILLISECONDS:
                expire = this.expire * 1000;
                break;
            default:
                expire = this.expire;
        }
        return expire;
    }


    @Override
    public int getExpireSecond() {
        return (int) this.getExpire();
    }

    @Override
    public long getExpireMs() {
        return this.getExpire();
    }
}
